package org.yaac.server.egql.evaluator.function;

import org.yaac.server.egql.evaluator.EvaluationResult;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.shared.ErrorCode;

import com.google.appengine.api.datastore.PostalAddress;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class PostalAddressFunction extends Function {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void validate() throws EGQLException {
		FunctionUtil.ensureParamSize(ops, 1);
	}
	
	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		EvaluationResult r = ops.get(0).evaluate(record);
		
		if (r.getPayload() instanceof String) {
			PostalAddress val = new PostalAddress((String) r.getPayload());
			return new EvaluationResult(val, r);	
		} else {
			return r.withWarning(ErrorCode.W130);
		}
	}
}
